package ui;


import java.util.ArrayList;

import operations.AramaOperation;
import operations.Makaleopenoperation;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.ColumnGenerator;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;

import db.Databasehelper;
import domain.Makaledomain;

public class AramaUi extends Panel implements Property.ValueChangeListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 9174976106830356348L;
    private NativeSelect aramaKriteri;
    private TextField aranacakMakale;
    private TextField aramaDeseni;
    private Table aramaSonuclariTable;
    
	public AramaUi() {
		initVerticalLayout();
	}

	private void initVerticalLayout() {
		FormLayout formLayout = new FormLayout();
        setContent(formLayout);
        formLayout.setSizeFull();
        
        aranacakMakale = new TextField("Aranacak Makale");
        aramaKriteri = new NativeSelect("Arama Kriteri");
        aramaDeseni= new TextField("Anahtar Kelime");
        Button arama = new Button("Ara");
        
        for (String aramaKriteri : AramaOperation.aramaKriterleri) {
			this.aramaKriteri.addItem(aramaKriteri);
		} 
        
        aramaKriteri.setNullSelectionAllowed(false);
        aramaKriteri.setValue(AramaOperation.aramaKriterleri[0]);
        aramaKriteri.setImmediate(true);
        
        aramaDeseni.setVisible(false);
        
        arama.addListener(new Button.ClickListener() {
			
			public void buttonClick(ClickEvent event) {
				aramaIslemi();
			}
		});
        
        addComponent(aranacakMakale);
        addComponent(aramaKriteri);
        addComponent(aramaDeseni);
        addComponent(arama);
	}

	private void aramaIslemi() {
		AramaOperation arama = new AramaOperation(aramaKriteri.getValue(),aranacakMakale.getValue().toString());
		
		ArrayList<Makaledomain> aramaSonuc =arama.aramaSonuclari();
		
		if(aramaSonuclariTable == null)
			aramaSonuclariTable = new Table("Arama Sonuclari");
		else
		{
			removeComponent(aramaSonuclariTable);
			aramaSonuclariTable = new Table("Arama Sonuclari");
		}
		
		aramaSonuclariTable.setSelectable(true);
		aramaSonuclariTable.setImmediate(true);
		aramaSonuclariTable.setWidth("100%");
		aramaSonuclariTable.setHeight("170px");
		
		final String aramaSonuclari[] = {"makaleadi","yazar","dergisi","konusu","durumu"};
		for (String kolon : aramaSonuclari) {
			aramaSonuclariTable.addContainerProperty(kolon, String.class, null);
		}
		
		aramaSonuclariTable.setColumnAlignments(new String[] { Table.ALIGN_LEFT,
                Table.ALIGN_RIGHT,Table.ALIGN_RIGHT,Table.ALIGN_RIGHT,Table.ALIGN_RIGHT});
		
		for (Makaledomain bulunanMakale : aramaSonuc) {
			aramaSonuclariTable.addItem(new String[]{
				bulunanMakale.getMakaleadi(),bulunanMakale.getYazar(),bulunanMakale.getDergiadi(),
				bulunanMakale.getKonu(),bulunanMakale.getDurum()},
				new Integer(bulunanMakale.getId()));
		}
		
		addComponent(aramaSonuclariTable);
	}

	public void valueChange(ValueChangeEvent event) {
		Makaleopenoperation secilenMakaleyiAcma = new Makaleopenoperation();
		Databasehelper db = new Databasehelper();
		
		String makaleurl = db.makaleurlgetir(event.getProperty().toString());
		String filename = db.makaledosyaadigetir(event.getProperty().toString());
		secilenMakaleyiAcma.makaleac(getWindow(), makaleurl, filename);
	}
}
